
class DisplayLogs < Controller
  def index e
    if e.params['detail']
      detail e
    else
      list e
    end
  end

  def list e
    if e.params['remove']
      DmLogger::LogFile.get!(e.params['remove']).destroy
    end


    data = <<JSCRIPT
<script>
  toggle_log = function(e) {
    var p = e;
    console.log(p.className);
    var c = p.className.split(' ');
    if (c[1] == "hide") {
      c[1] = "show";
    } else {
      c[1] = "hide";
    }
    console.log(c, c.join(' '));
    p.className = c.join(' ');
  }
  show_log = function(e, id) {
    if (!e.displayingData) {
      e.displayingData = true;
      var data = document.createElement('div');
      data.className = 'data toggle';
      data.innerHTML = '<iframe '+
          'src="/admin/logs?detail='+id+'"'+
          'width="100%" height="500px"></iframe>';
      e.appendChild(data);
    } else {
      toggle_log(e);
    }
    return false;
  }
</script>
<style>
  .log {
    border-bottom:1px solid silver;
    clear:both;
  }
  .first .log {
    /*border:1px solid silver;*/
  }
  .log .title {
    padding-left:1em;
    padding-right:1em;
    font-size:1em;
    margin:0.3em;
  }
  .log .title {
    display:block;
    width:80%;
    float:left;
  }
  .log .data {
    padding:0;
    margin:0;
  }
  .log a {
    text-decoration:none;
    color:black;
    line-height:180%;
  }
  .log .remove {
    display:block;
    float:right;
    margin-right:0.5em;
  }
  .hide .toggle {
      display:none;
  }
  .data iframe { 
    border:none;
  }

</style>
JSCRIPT

    i = 0
    DmLogger::LogFile.all(:limit => 20, :order => [:id.desc]).each { |file|
      if i == 0
        data << '<div class="first">'
      end
      data << '
      <div class="log show">
        <div>
          <a href="#" class="title" onclick="return show_log(this.parentNode.parentNode, '+file.id.to_s+')">'+file.message+'</span>
          <a href="?remove='+file.id.to_s+'" class="remove">delete</a>
        </div>
      </div>'
      if i == 0
        data << '</div>'
      end
      i += 1
    }
    data
  end

  def detail e
    e.respond "", {
      :response => DmLogger::LogFile.get(e.params['detail']).data
    }
  end

end
